/**
 * Created by taohj on 2014/8/15.
 */
var org = new Object();


/**
 * 模块列表
 * */
org.initgrid = function () {


    org.datagrid = $("#admin_org_list_datagrid");


    org.datagrid.datagrid({
        url: '/admin/org/list',
        singleSelect: true,
        collapsible: true,
        pagination: true,
        rownumbers: true,
        fitColumns: true,
        sortName: "id",
        sortOrder: "desc",
        checkOnSelect:false,
        selectOnCheck:false,
        fit: true,
        pageList: [20, 50, 100, 200],
        pageSize: 20,
        toolbar: [
            {
                iconCls: 'icon-add',
                text: '新增',
                handler: function () {
                    var createDialog = $("<div></div>");
                    createDialog.dialog({
                        title: '新增',
                        href: '/admin/org/createV',
                        modal: true,
                        width: 400,
                        top: 150
                    });
                }
            },
            '-',
            {
                iconCls: 'icon-edit',
                text: '更新',
                handler: function () {
                    var selectRow = org.datagrid.datagrid("getSelected");
                    if (!selectRow) {
                        //todo
                        return;
                    }

                    var  id = selectRow.id;
                    var createDialog = $("<div></div>");
                    createDialog.dialog({
                        title: '更新',
                        href: '/admin/org/modifyV' + '?id=' + id,
                        modal: true,
                        width: 400,
                        top: 150
                    });

                }
            },
            '-',
            {
                iconCls: 'icon-remove',
                text: '删除',
                handler: function () {

                    var selectRow = org.datagrid.datagrid("getSelected");
                    if (!selectRow) {
                        //todo
                        return;
                    }

                    $.messager.confirm('删除确认', '确定要删除此项么？ ', function (r) {
                        if (r) {
                            $.post('/admin/org/remove', {
                                id: selectRow.id
                            }, function (result) {
                                util.requestUtil(result, null, org.datagrid);
                            });
                        }
                    });
                }
            },'-',{
                iconCls: 'icon-remove',
                text: '通过',
                handler: function () {

                    var orgs=new Array();
                    var checkeds=org.datagrid.datagrid('getChecked');
                    for(var i= 0;i<checkeds.length;i++){
                        var row=checkeds[i];
                        orgs[i]=row.id;
                    }
                    $.post("admin/org/audit", { 'orgs[]': orgs,auditStatus:183},function(result){
                        util.requestUtil(result, null, org.datagrid)
                    });

                }
            },'-',{
                iconCls: 'icon-remove',
                text: '不通过',
                handler: function () {

                    var orgs=new Array();
                    var checkeds=org.datagrid.datagrid('getChecked');
                    for(var i= 0;i<checkeds.length;i++){
                        var row=checkeds[i];
                        orgs[i]=row.id;
                    }
                    $.post("admin/org/audit", { 'orgs[]': orgs,auditStatus:182},function(result){
                        util.requestUtil(result, null, org.datagrid)
                    });

                }
            }
        ],
        columns: [
            [
                {field: "id_ck", width: 60, checkbox: true,formatter:function(value,row,index){return row.id;}},
                {field: "id", title: "编号", width: 60, sortable: true},
                {field: "name", title: "名称", width: 200, sortable: true},
                {field: "headimage", title: "图片", width: 80, sortable: true, formatter: function (value, row, index) {

                    return value ? '<img style="width:26px; height:26px; margin-top:2px;  margin-buttom:2px;" src="' + value + '" >' : '无';
                }    },
                {field: "superOrg", title: "上级机构", width: 200, sortable: true, formatter: function (value, row, index) {
                    return value ? value.name : '';
                }},
                {field: "contacts", title: "联系人", width: 200, sortable: true},
                {field: "telephone", title: "联系电话", width: 200, sortable: true},
                {field: "email", title: "电子邮件", width: 200, sortable: true},
                {field: "address", title: "地址", width: 200, sortable: true},
                {field: "postcode", title: "邮编", width: 200, sortable: true},
                {field: "category", title: "机构分类", width: 200, sortable: true, formatter: function (value, row, index) {
                    return value ? value.name : '';

                }},
                {field: "auditStatus", title: "审批状态", width: 200, sortable: true,formatter:function(value){return value?value.name:'';}},
                {field: "status", title: "状态", width: 200, sortable: true,formatter:function(value){return value?value.name:'';}},
                {field: "updater", title: "修改人", width: 200, sortable: true},

                {field: "updateTime", title: "修改时间", width: 200, sortable: true},
                {field: "description", title: "简介"}
            ]
        ]
    });
};


//新建提交
org.createSubmit = function (button) {
    var form = util.form(button);
    form.form('submit',{
        url: '/admin/org/create',
        success: function (result) {
            var dialog = $(button).closest("div.window-body");
            util.requestUtil(result, dialog, org.datagrid)
        }
    });

};


//修改提交
org.modifySubmit = function (button) {
    var form = util.form(button);
    form.form('submit',{
        url: '/admin/org/modify',
        success: function (result) {
            var dialog = $(button).closest("div.window-body");
            util.requestUtil(result, dialog, org.datagrid)
        }
    });
};


//搜索
org.searchSubmit = function (button) {
    var form = util.form(button);
    var params = util.serializeObject(form);
    org.datagrid.datagrid("load", params);
}

